package com.ruoyi.project.goods.order.controller;

import java.util.List;

import com.ruoyi.project.goods.order.domain.GoodsOrderHead;
import com.ruoyi.project.goods.order.domain.GoodsOrderModel;
import com.ruoyi.project.goods.order.service.IGoodsOrderHeadService;
import com.ruoyi.project.system.dict.utils.DictUtils;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.goods.order.domain.GoodsOrder;
import com.ruoyi.project.goods.order.service.IGoodsOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author lonsn
 * @date 2020-11-12
 */
@Controller
@RequestMapping("/goods/order")
public class GoodsOrderController extends BaseController
{
    private String prefix = "goods/order";

    @Autowired
    private IGoodsOrderService goodsOrderService;
    @Autowired
    private IGoodsOrderHeadService goodsOrderHeadService;

    /**
     * 销售页面
     */
    @GetMapping("/sale")
    public String sale()
    {
        return prefix + "/sale";
    }

    //跳转 订单管理-To店长
    @GetMapping("/orderDianzhang")
    public String orderDianzhang()
    {
        return prefix + "/order_dianzhang";
    }

    @RequiresPermissions("goods:order:view")
    @GetMapping()
    public String order(){
        return prefix + "/order";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("goods:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsOrder goodsOrder)
    {
        User user=getSysUser();
        goodsOrder.setCreateBy(user.getLoginName());
        startPage();
        List<GoodsOrder> list = goodsOrderService.selectGoodsOrderList(goodsOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("goods:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsOrder goodsOrder)
    {
        User user=getSysUser();
        goodsOrder.setCreateBy(user.getLoginName());
        List<GoodsOrder> list = goodsOrderService.selectGoodsOrderList(goodsOrder);
        ExcelUtil<GoodsOrder> util = new ExcelUtil<GoodsOrder>(GoodsOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单;下单★★★★★
     */
    @RequiresPermissions("goods:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsOrderModel goodsOrderModel)
    {
        User user= getSysUser();
        goodsOrderModel.setCreateBy(user.getLoginName());
        goodsOrderModel.setDeptId(user.getDeptId());
        goodsOrderService.insertGoodsOrder(goodsOrderModel);
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("orderNo", goodsOrderModel.getOrderNo());
        ajax.put("orderMoney", goodsOrderModel.getOrderMoney());
        return ajax;
    }

    /**
     * 修改订单
     */
    @GetMapping("/edit/{orderNo}")
    public String edit(@PathVariable("orderNo") String orderNo, ModelMap mmap)
    {
        GoodsOrder goodsOrder = goodsOrderService.selectGoodsOrderByOrderNo(orderNo);
        mmap.put("goodsOrder", goodsOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("goods:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsOrder goodsOrder)
    {
        return toAjax(goodsOrderService.updateGoodsOrder(goodsOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("goods:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(goodsOrderService.deleteGoodsOrderByIds(ids));
    }


    /**
     * 下单成功后，调整到订单管理页面
     */
    @GetMapping("/orderDetail/{orderNo}")
    public String orderDetail(@PathVariable("orderNo") String orderNo, ModelMap mmap)
    {
        GoodsOrder goodsOrder = goodsOrderService.selectGoodsOrderByOrderNo(orderNo);
        mmap.put("goodsOrder", goodsOrder);
        User user=getSysUser();
        mmap.put("deptAddress",user.getDeptAddress());
        return prefix + "/orderDetail";
    }

    /**
     * 确认收款
     */
    @RequiresPermissions("goods:order:paySuccess")
    @Log(title = "收款", businessType = BusinessType.UPDATE)
    @PostMapping( "/paySuccess")
    @ResponseBody
    public AjaxResult paySuccess(String orderNo,String payChannelId)
    {
        User user=getSysUser();
        GoodsOrder order=new GoodsOrder();
        order.setOrderNo(orderNo);
        order.setPayChannelId(Integer.parseInt(payChannelId));
        goodsOrderService.paySuccess(order);
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("msg", "200");
        return AjaxResult.success("操作成功");
    }

    /**
     * 打印小票
     */
    @GetMapping("/orderPrint/{orderNo}")
    public String orderPrint(@PathVariable("orderNo") String orderNo, ModelMap mmap) {
        User user=getSysUser();
        GoodsOrder goodsOrder = goodsOrderService.selectGoodsOrderByOrderNo(orderNo);
        GoodsOrderHead goodsOrderHead =new GoodsOrderHead();
        goodsOrderHead.setOrderNo(orderNo);
        List<GoodsOrderHead> list= goodsOrderHeadService.selectGoodsOrderHeadList(goodsOrderHead);
        mmap.put("goodsOrder", goodsOrder);
        mmap.put("listOrderHead", list);
        mmap.put("dept",user.getDept());
        mmap.put("deptAddress",user.getDeptAddress());
        return prefix + "/orderPrint";
    }

    /**
     * 查询订单列表-店长
     */
    @RequiresPermissions("goods:order:list_dianzhang")
    @PostMapping("/list_dianzhang")
    @ResponseBody
    public TableDataInfo list_dianzhang(GoodsOrder goodsOrder)  {
        User user=getSysUser();
        goodsOrder.setDeptId(user.getDeptId());
        startPage();
        List<GoodsOrder> list = goodsOrderService.selectGoodsOrderList(goodsOrder);
        return getDataTable(list);
    }
}
